<script setup>
import { useRoute } from 'vue-router';
import { ref, computed } from 'vue';

const props = defineProps({
  title: {
    // 标题
    type: String,
    default: ''
  },
  titleSize: {
    // 标题
    type: String,
    default: '18px'
  },
  back: {
    type: Boolean,
    default: false
  },
  backText: {
    // 位置在左
    type: String,
    default: '返回'
  },
  backTxtColor: {
    type: String,
    default: ''
  },
  TextColor: {
    type: String,
    default: ''
  },
  fotSize: {
    // 默认大小
    type: String,
    default: '16px'
  },
  rightText: {
    type: String,
    default: ''
  }
});
const emit = defineEmits(['clickLeft', 'clickRight']);
const route = useRoute();
const defaultTitle = ref(route.meta.title);
const clickLeft = () => {
  emit('clickLeft');
};
const clickRight = () => {
  emit('clickRight');
};
</script>

<template>
  <div class="page-header">
    <div class="cell" @click="clickLeft">
      <div v-if="back" class="back">
        <p
          class="back_icon"
          :style="{
            width: '12px',
            height: '12px',
            borderRadius: '2px',
            borderLeft: `2px solid ${backTxtColor ? backTxtColor : TextColor}`,
            borderTop: `2px solid ${backTxtColor ? backTxtColor : TextColor}`,
            color: backTxtColor ? backTxtColor : TextColor
          }"
        />
        <span
          :style="{
            marginLeft: '-2px',
            color: backTxtColor ? backTxtColor : TextColor,
            fontSize: '14px'
          }"
        >
          {{ backText }}
        </span>
      </div>
      <slot name="left" v-else></slot>
    </div>
    <div class="title" :style="{ fontSize: titleSize }">{{ title || defaultTitle }}</div>
    <div class="cell" @click="clickRight">
      <span
        v-if="rightText"
        :style="{
          color: TextColor,
          fontSize: fotSize
        }"
      >
        {{ rightText }}
      </span>
      <slot v-else name="right"></slot>
    </div>
  </div>
</template>

<style lang="less" scoped>
.page-header {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 5fr 1fr;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 50px;
  padding: 0;
  margin: 0;
  user-select: none;
  .title {
    flex: 1;
    padding: 12px 0;
    font-weight: bold;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .cell {
    height: 50px; // 添加默认高度
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;

    .back {
      display: flex;
      align-items: center;
      justify-content: center;

      .back_icon {
        font-weight: bold;
        transform: rotate(-45deg);
      }
    }
  }
}
</style>
